* Begin log file
capture log close sublog
log using "$projdir/log/2_qcew_stack.txt", name(sublog) text replace

*-------------------------------------------------------------------------------
* Description: Stack and average the uncorrected, and bias-corrected, and 
* pandemic-corrected synthetic control results for various groupings of interest
* Author: Justin Wiltshire (edited by Denis Sosinskiy)
*
* Updated: May 10, 2024
*-------------------------------------------------------------------------------
clear

tempfile core
tempfile core0

* All treated counties
foreach y in employment avg_wkly_wage {	
	local n = 722513
		
		* Ensure necessary folders exists
		capture mkdir "$projdir/dta/analysis/qcew/`y'/naics`n'/agg_att"
		capture mkdir "$projdir/dta/analysis/qcew/`y'/naics`n'/agg_att/bias_corrected"
		capture mkdir "$projdir/dta/analysis/qcew/`y'/naics`n'/agg_att/covid_corrected"
		
		* Add trunit and trperiod to the uncorrected individual estimates if needed
		foreach z in California NY {
		
			* Set trperiod macro
			local _trp = 218
			if "`z'" == "NY" {
				local _trp = 216
			}
		
			* Get filelist
			local filelist: dir "$projdir/dta/analysis/qcew/`y'/naics`n'/`z'" files "*.dta"
			
			* Add the variables if needed
			foreach f of local filelist {
				if strpos("`f'", "_ate") == 0 {
					qui use "$projdir/dta/analysis/qcew/`y'/naics`n'/`z'/`f'", clear
					sort cty_fips _Co_Number
					
					* Extract and create trunit
					capture assert trunit
					if _rc != 0 {
						local _trunit = subinstr("`f'", "tr_", "", .)
						local _trunit = subinstr("`_trunit'", ".dta", "", .)
						qui gen trunit = `_trunit'
					}
					* Exclude mid counties in NY
					if !inlist(`_trunit', 36007, 36027, 36063, 36065, 36069, 36083, 36085, 36087, 36091, 36093, 36111) {
						local inclunits "`inclunits' `_trunit'"
					}
					
					* Generate trperiod
					capture assert trperiod
					if _rc != 0 {
						qui gen trperiod = `_trp'
					}
					
					* Add pop10 variable if needed
					capture assert pop10
					if _rc != 0 & !inlist(`_trunit', 36007, 36027, 36063, 36065, 36069, 36083, 36085, 36087, 36091, 36093, 36111) {
						save `core', replace
						use "$projdir/dta/analysis/qcew/`y'/naics`n'/`z'/corrected/`f'", clear
						keep cty_fips pop10
						collapse (mean) pop10, by(cty_fips)
						save `core0', replace
						use `core', clear
						merge m:1 cty_fips using `core0', nogen keep(1 3)
					}

					
					* Save the file
					qui save "$projdir/dta/analysis/qcew/`y'/naics`n'/`z'/`f'", replace
				}
			}
		}

		* Uncorrected estimates
		#delimit ;
			stackscpvals, 
				gap(gap) 
				time(_time) 
				unit(cty_fips) 
				pvalues(rmspe variance) 
				avgwts(pop10)
				emin(-17) emax(33) 
				filepath("$projdir/dta/analysis/qcew/`y'/naics`n'/California") 
				filepath_2("$projdir/dta/analysis/qcew/`y'/naics`n'/NY")
				savepath("$projdir/dta/analysis/qcew/`y'/naics`n'/agg_att")
				savename("att")
				keeptrunits(`inclunits');
			#delimit cr
		
		* Bias-corrected estimates
		#delimit ;
			stackscpvals, 
				gap(gap_bc) 
				time(_time) 
				unit(cty_fips) 
				pvalues(rmspe variance) 
				avgwts(pop10)
				emin(-17) emax(33) 
				filepath("$projdir/dta/analysis/qcew/`y'/naics`n'/California") 
				filepath_2("$projdir/dta/analysis/qcew/`y'/naics`n'/NY")
				savepath("$projdir/dta/analysis/qcew/`y'/naics`n'/agg_att/bias_corrected")
				savename("bias_corr_att")
				keeptrunits(`inclunits');
			#delimit cr
		
		* Covid-corrected estimates
		#delimit ;
			stackscpvals, 
				gap(p_gap) 
				time(_time) 
				unit(cty_fips) 
				pvalues(rmspe variance) 
				avgwts(pop10)
				emin(-17) emax(33) 
				filepath("$projdir/dta/analysis/qcew/`y'/naics`n'/California/corrected") 
				filepath_2("$projdir/dta/analysis/qcew/`y'/naics`n'/NY/corrected")
				savepath("$projdir/dta/analysis/qcew/`y'/naics`n'/agg_att/covid_corrected")
				savename("covid_corr_att")
				keeptrunits(`inclunits');
			#delimit cr
}

* Excluding treated counties in the SF Bay Area and NYC
foreach y in employment avg_wkly_wage {	
	local n = 722513
		
		* Ensure necessary folders exists
		capture mkdir "$projdir/dta/analysis/qcew/`y'/naics`n'/nobaynyc_att"
		capture mkdir "$projdir/dta/analysis/qcew/`y'/naics`n'/nobaynyc_att/bias_corrected"
		capture mkdir "$projdir/dta/analysis/qcew/`y'/naics`n'/nobaynyc_att/covid_corrected"
		
		* Generate trunit and trperiod variables if needed, and build inclunits list
		local inclunits
		foreach z in California NY {
		
			* Set trperiod macro
			local _trp = 218
			if "`z'" == "NY" {
				local _trp = 216
			}
		
			* Get filelist
			local filelist: dir "$projdir/dta/analysis/qcew/`y'/naics`n'/`z'" files "*.dta"
			
			* Add the variables if needed, and add the appropriate counties to the macro
			foreach f of local filelist {
				if strpos("`f'", "_ate") == 0 {
					qui use "$projdir/dta/analysis/qcew/`y'/naics`n'/`z'/`f'", clear
					
					* Extract and create trunit (if necessary) and add to inclunits list
					local _trunit = subinstr("`f'", "tr_", "", .)
					local _trunit = subinstr("`_trunit'", ".dta", "", .)
					capture assert trunit
					if _rc != 0 {
						qui gen trunit = `_trunit'
					}
					* Exclude counties in SF Bay Area and NYC
					if !inlist(`_trunit', 6001, 6013, 6041, 6055, 6075, 6081, 6085, 6095, 6097, 36005, 36047, 36061, 36081, 36085, ///
											36007, 36027, 36063, 36065, 36069, 36083, 36085, 36087, 36091, 36093, 36111) {
						local inclunits "`inclunits' `_trunit'"
					}
					
					* Generate trperiod
					capture assert trperiod
					if _rc != 0 {
						qui gen trperiod = `_trp'
					}
					
					* Add pop10 variable if needed
					capture assert pop10
					if _rc != 0 & !inlist(`_trunit', 36007, 36027, 36063, 36065, 36069, 36083, 36085, 36087, 36091, 36093, 36111) {
						save `core', replace
						use "$projdir/dta/analysis/qcew/`y'/naics`n'/`z'/corrected/`f'", clear
						keep cty_fips pop10
						collapse (mean) pop10, by(cty_fips)
						save `core0', replace
						use `core', clear
						merge m:1 cty_fips using `core0', nogen keep(1 3)
					}
					
					* Save the file
					qui save "$projdir/dta/analysis/qcew/`y'/naics`n'/`z'/`f'", replace
				}
			}
		}
		
		* Uncorrected estimates
		#delimit ;
			stackscpvals, 
				gap(gap) 
				time(_time) 
				unit(cty_fips) 
				pvalues(rmspe variance) 
				avgwts(pop10)
				emin(-17) emax(33) 
				filepath("$projdir/dta/analysis/qcew/`y'/naics`n'/California") 
				filepath_2("$projdir/dta/analysis/qcew/`y'/naics`n'/NY")
				savepath("$projdir/dta/analysis/qcew/`y'/naics`n'/nobaynyc_att")
				savename("att")
				keeptrunits(`inclunits');
			#delimit cr
		
		* Bias-corrected estimates
		#delimit ;
			stackscpvals, 
				gap(gap_bc) 
				time(_time) 
				unit(cty_fips) 
				pvalues(rmspe variance) 
				avgwts(pop10)
				emin(-17) emax(33) 
				filepath("$projdir/dta/analysis/qcew/`y'/naics`n'/California") 
				filepath_2("$projdir/dta/analysis/qcew/`y'/naics`n'/NY")
				savepath("$projdir/dta/analysis/qcew/`y'/naics`n'/nobaynyc_att/bias_corrected")
				savename("bias_corr_att")
				keeptrunits(`inclunits');
			#delimit cr
		
		* Covid-corrected estimates
		#delimit ;
			stackscpvals, 
				gap(p_gap) 
				time(_time) 
				unit(cty_fips) 
				pvalues(rmspe variance) 
				avgwts(pop10)
				emin(-17) emax(33) 
				filepath("$projdir/dta/analysis/qcew/`y'/naics`n'/California/corrected") 
				filepath_2("$projdir/dta/analysis/qcew/`y'/naics`n'/NY/corrected")
				savepath("$projdir/dta/analysis/qcew/`y'/naics`n'/nobaynyc_att/covid_corrected")
				savename("covid_corr_att")
				keeptrunits(`inclunits');
			#delimit cr
}

* Excluding treated counties with local minimum wages
foreach y in employment avg_wkly_wage {	
	local n = 722513
		
		* Ensure necessary folders exists
		capture mkdir "$projdir/dta/analysis/qcew/`y'/naics`n'/nolocal_att"
		capture mkdir "$projdir/dta/analysis/qcew/`y'/naics`n'/nolocal_att/bias_corrected"
		capture mkdir "$projdir/dta/analysis/qcew/`y'/naics`n'/nolocal_att/covid_corrected"
		
		* Generate trunit and trperiod variables if needed, and build inclunits list
		local inclunits
		foreach z in California NY {
		
			* Set trperiod macro
			local _trp = 218
			if "`z'" == "NY" {
				local _trp = 216
			}
		
			* Get filelist
			local filelist: dir "$projdir/dta/analysis/qcew/`y'/naics`n'/`z'" files "*.dta"
			
			* Add the variables if needed, and add the appropriate counties to the macro
			foreach f of local filelist {
				if strpos("`f'", "_ate") == 0 {
					qui use "$projdir/dta/analysis/qcew/`y'/naics`n'/`z'/`f'", clear
					
					* Extract and create trunit (if necessary) and add to inclunits list
					local _trunit = subinstr("`f'", "tr_", "", .)
					local _trunit = subinstr("`_trunit'", ".dta", "", .)
					capture assert trunit
					if _rc != 0 {
						qui gen trunit = `_trunit'
					}
					* Exclude counties with local minimum wages
					if !inlist(`_trunit', 6001, 6013, 6037, 6041, 6073, 6075, 6081, 6085, 6097, 36005, 36047, 36061, 36081, 36085, ///
											36007, 36027, 36063, 36065, 36069, 36083, 36085, 36087, 36091, 36093, 36111) {
						local inclunits "`inclunits' `_trunit'"
					}
					
					* Generate trperiod
					capture assert trperiod
					if _rc != 0 {
						qui gen trperiod = `_trp'
					}
					
					* Add pop10 variable if needed
					capture assert pop10
					if _rc != 0 & !inlist(`_trunit', 36007, 36027, 36063, 36065, 36069, 36083, 36085, 36087, 36091, 36093, 36111) {
						save `core', replace
						use "$projdir/dta/analysis/qcew/`y'/naics`n'/`z'/corrected/`f'", clear
						keep cty_fips pop10
						collapse (mean) pop10, by(cty_fips)
						save `core0', replace
						use `core', clear
						merge m:1 cty_fips using `core0', nogen keep(1 3)
					}
					
					* Save the file
					qui save "$projdir/dta/analysis/qcew/`y'/naics`n'/`z'/`f'", replace
				}
			}
		}
		
		* Uncorrected estimates
		#delimit ;
			stackscpvals, 
				gap(gap) 
				time(_time) 
				unit(cty_fips) 
				pvalues(rmspe variance) 
				avgwts(pop10)
				emin(-17) emax(33) 
				filepath("$projdir/dta/analysis/qcew/`y'/naics`n'/California") 
				filepath_2("$projdir/dta/analysis/qcew/`y'/naics`n'/NY")
				savepath("$projdir/dta/analysis/qcew/`y'/naics`n'/nolocal_att")
				savename("att")
				keeptrunits(`inclunits');
			#delimit cr
		
		* Bias-corrected estimates
		#delimit ;
			stackscpvals, 
				gap(gap_bc) 
				time(_time) 
				unit(cty_fips) 
				pvalues(rmspe variance) 
				avgwts(pop10)
				emin(-17) emax(33) 
				filepath("$projdir/dta/analysis/qcew/`y'/naics`n'/California") 
				filepath_2("$projdir/dta/analysis/qcew/`y'/naics`n'/NY")
				savepath("$projdir/dta/analysis/qcew/`y'/naics`n'/nolocal_att/bias_corrected")
				savename("bias_corr_att")
				keeptrunits(`inclunits');
			#delimit cr
		
		* Covid-corrected estimates
		#delimit ;
			stackscpvals, 
				gap(p_gap) 
				time(_time) 
				unit(cty_fips) 
				pvalues(rmspe variance) 
				avgwts(pop10)
				emin(-17) emax(33) 
				filepath("$projdir/dta/analysis/qcew/`y'/naics`n'/California/corrected") 
				filepath_2("$projdir/dta/analysis/qcew/`y'/naics`n'/NY/corrected")
				savepath("$projdir/dta/analysis/qcew/`y'/naics`n'/nolocal_att/covid_corrected")
				savename("covid_corr_att")
				keeptrunits(`inclunits');
			#delimit cr
}

* Close the log
log close sublog
